<template>
  <div class="my-container">
    <!-- 已登录 -->
    <div v-if="user" class="header userinfo">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            fit="cover"
            round
            :src="userinfo.photo"
          />
          <span class="name">{{userinfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round @click="$router.push('/user/profile')">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <div class="count">{{userinfo.art_count}}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">{{userinfo.follow_count}}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">{{userinfo.fans_count}}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">{{userinfo.like_count}}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="login-img" src="~@/assets/images/mobile.png" alt="">
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <template v-slot:default>
          <i class="iconfont icon-shoucang"></i>
          <span class="text">收藏</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template v-slot:default>
          <i class="iconfont icon-lishi"></i>
          <span class="text">历史</span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- 下三板 -->
    <van-cell title="消息通知" is-link/>
    <van-cell title="小智同学" is-link/>
    <van-cell v-if="user" title="退出登录" class="lgout-cell" clickable @click="onLogout"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      userinfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  mounted () {},
  methods: {
    onLogout () {
      this.$dialog.confirm({
        title: '退出登录'
      })
        .then(() => {
        // on confirm
          this.$store.commit('removeUser')
        })
        .catch(() => {
        // on cancel
        })
      //
    },
    async loadUserInfo () {
      try {
        const res = await getUserInfo()
        this.userinfo = res.data.data
      } catch (err) {
        console.log('获取用户信息失败,请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .my-container{
    .header{
      height: 361px;
      background: url('~@/assets/images/banner.png');
      background-size: cover;
    }
    .not-login{
      display: flex;
      justify-content: center;
      align-items: center;
      .login-btn{
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .login-img{
          width: 132px;
          height: 132px;
          margin-bottom: 15px;
        }
        .text{
          font-size: 28px;
          color: #fff;
        }
      }
    }
    .userinfo{
      .base-info{
        height: 231px;
        padding: 76px 32px 23px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left{
          display: flex;
          align-items: center;
          .avatar{
            width: 132px;
            height: 132px;
            margin-right: 23px;
            border: 4px solid #fff;
          }
          .name{font-size: 30px;color: #fff;}
        }
      }
      .data-stats{
        height: 130px;
        display: flex;
        .data-item{
          flex: 1;
          display: flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
          color: #fff;
          .count{font-size: 36px;}
          .text{font-size: 24px;}
        }
      }
    }
    .grid-nav{
      margin-bottom: 9px;
      .grid-item{
        height: 141px;
        i.iconfont{
          font-size: 45px;
        }
        .icon-shoucang{
          color: #eb5253;
        }
        .icon-lishi{
          color: #ff9d1d;
        }
        span.text{
          font-size: 28px;
        }
      }
    }
    .lgout-cell{
      color: #d86262;
      text-align: center;
      margin-top: 9px;
    }
  }
</style>
